﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQWEB.administrator.sources;
using System.Data;
using System.Web.Script.Serialization;
using System.Web.Routing;
using VQWEB.common;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for ConfigsHandler
    /// </summary>
    public class core_ConfigHandler : IHttpHandler, IRouteHandler
    {
        ConfigData ConfigsData = new ConfigData();
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
               

                //context.Response.End();

                string show_configs = context.Request["show_configs"];
                if (!string.IsNullOrEmpty(show_configs))
                {
                    DataTable table = ConfigsData.ViewConfigs();
                    string json = HelperData.ToJSONString(table);
                    context.Request.ContentType = "application/json";
                    context.Response.Write(json);

                }

                string update_configs = context.Request["update_configs"];
                if (!string.IsNullOrEmpty(update_configs))
                {
                    try
                    {
                        update_configs = HttpUtility.UrlDecode(update_configs, System.Text.UTF8Encoding.Default);
                        Dictionary<string, string> received = serializer.Deserialize<Dictionary<string, string>>(update_configs);
                        if (!received.ContainsKey("close_website"))
                            received.Add("close_website", "False");
                        if (!received.ContainsKey("webmail_enablessl"))
                            received.Add("webmail_enablessl", "False");

                        ConfigsData.UpdateConfigs(received);

                        DataTable table = ConfigsData.ViewConfigs();
                        string json = HelperData.ToJSONString(table);
                        context.Request.ContentType = "application/json";
                        context.Response.Write(json);
                   
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
           
        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}